package com.unisyou.dom;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;

public class Demo1 {

    public static void main(String[] args) {
        String str = "<?xml version='1.0' encoding='utf-8' standalone='yes' ?><smsinfos><smsinfo><address></address><date>1572217350323</date><type>3</type><body>2019年诺贝尔经济学奖，阿比吉特·巴纳吉(Abhijit Banerjee)、埃丝特·迪弗洛(Esther Duflo)和迈克尔·克雷默(Michael Kremer)——“在减轻全球贫困方面的实验性做法”。这本书必须读啊</body><person></person><read>1</read></smsinfo><smsinfo><address></address><date>1571051758435</date><type>3</type><body>2019年诺贝尔经济学奖阿比吉特·巴纳吉Abhijit Banerjee埃丝特·迪弗洛Esther Duflo和迈克尔·克雷默Michael Kremer在减轻全球贫困方面的实验性做法这本书必须读啊</body><person></person><read>1</read></smsinfo><smsinfo><address>1065972901</address><date>1564017554000</date><type>1</type><body>天翼安全提醒：您的来电17139417014为高频呼叫，请谨慎接听！【广东电信&号码百事通】</body><person>0</person><read>1</read></smsinfo><smsinfo><address>10086055</address><date>1558255380000</date><type>1</type><body>【在路上，阅读悦听】福利到！2019年6月30日前使用“咪咕阅读”APP ，即可获得30天至尊会员，《冰与火之歌》、《美国XJ》等热门畅销图书免费看；30G咪咕定向流量免费领；次月更有机会获得个性化听书30天权益。猛戳http://wap.cmread.com/r/p/gdczhy.jsp?cm=S20000PG&vt=3领取。【中国移动】</body><person>0</person><read>1</read></smsinfo></smsinfos>";
        str = str.replace("&","&amp;");
        System.out.println(str);
        stringToXmlByDom4j(str);
    }


    public static HashMap<String, Object> stringToXmlByDom4j(String content){
        HashMap<String, Object> result = new HashMap<String, Object>();

        try {
            SAXReader saxReader=new SAXReader();
            org.dom4j.Document docDom4j=saxReader.read(new ByteArrayInputStream(content.getBytes("utf-8")));
            org.dom4j.Element root = docDom4j.getRootElement();
            List<Attribute> rooAttrList = root.attributes();
            for (Attribute rootAttr : rooAttrList) {
                System.out.println(rootAttr.getName() + ": " + rootAttr.getValue());
                result.put(rootAttr.getName(), rootAttr.getValue());
            }

            List<org.dom4j.Element> childElements = root.elements();

            for (org.dom4j.Element e1 : childElements) {
                System.out.println("第一层："+e1.getName() + ": " + e1.getText());
                result.put(e1.getName(), e1.getText());
            }

            for (org.dom4j.Element child : childElements) {
                //未知属性名情况下
                List<Attribute> attributeList = child.attributes();
                for (Attribute attr : attributeList) {
                    System.out.println("第二层："+attr.getName() + ": " + attr.getValue());
                    result.put(attr.getName(), attr.getValue());
                }

                //已知属性名情况下
//           System.out.println("id: " + child.attributeValue("id"));

                //未知子元素名情况下
                List<Element> elementList = child.elements();
                for (org.dom4j.Element ele : elementList) {

                    System.out.println("第二层："+ele.getName() + ": " + ele.getText());
                    result.put(ele.getName(), ele.getText());

                    List<Attribute> kidAttr = ele.attributes();
                    for (Attribute kidattr : kidAttr) {
                        System.out.println("第三层："+kidattr.getName() + ": " + kidattr.getValue());
                        result.put(kidattr.getName(), kidattr.getValue());
                    }

                    List<org.dom4j.Element> lidList = ele.elements();
                    int size = lidList.size();
                    if(size>0){
                        for (org.dom4j.Element e2 : lidList) {
                            System.out.println("第三层："+e2.getName() + ": " + e2.getText());
                            result.put(e2.getName(), e2.getText());
                        }
                    }

                }
//           System.out.println();

                //已知子元素名的情况下
//           System.out.println("title" + child.elementText("title"));
//           System.out.println("author" + child.elementText("author"));
                //这行是为了格式化美观而存在
//           System.out.println();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
